<script setup>
import { ref, computed } from "vue";
import { store } from '@/store'
import { useRouter, useRoute } from 'vue-router'

const router = useRouter()
const route = useRoute()

const asideWidth = computed(() => store.isCollapse ? '64px' : '200px')
const activeMenu = computed(() => route.path)

const menuList = ref([
  {
    label: '控制台',
    icon: 'HomeFilled',
    path: '/dashboard',
  },
  {
    label: "系统管理",
    icon: "Setting",
    children: [
      {
        label: "用户管理",
        icon: "UserFilled",
        path: "/user",
      },
      {
        label: "角色管理",
        icon: "UserFilled",
        path: "/role",
      },
      {
        label: "分类管理",
        icon: "UserFilled",
        path: "/category",
      }
    ]
  },
  {
    label: "财务管理",
    icon: "WalletFilled",
    children: [
      {
        label: "财务报表",
        icon: "UserFilled",
        path: "/report",
      },
      {
        label: "收入明细",
        icon: "UserFilled",
        path: "/income",
      },
      {
        label: "支出明细",
        icon: "UserFilled",
        path: "/expense",
      },
      {
        label: "转账明细",
        icon: "UserFilled",
        path: "/transfer",
      },
      {
        label: "债务管理",
        icon: "UserFilled",
        path: "/debt",
      },
      {
        label: "账户类型",
        icon: "UserFilled",
        path: "/fundAccountType",
      },
      {
        label: "资金账户",
        icon: "UserFilled",
        path: "/fundAccount",
      },
      {
        label: "数据备份与恢复",
        icon: "UserFilled",
        path: "/backup",
      }
    ]
  }
])

const handleMenu = (item) => {
  router.push(item.path)
}
</script>

<template>
  <el-aside :width="asideWidth">
    <el-menu
        active-text-color="#389fff"
        background-color="#545c64"
        :default-active="activeMenu"
        text-color="#fff"
        :collapse="store.isCollapse"
        :collapse-transition="false"
        :unique-opened="true"
    >
      <h1 v-show="!store.isCollapse" class="mb-2">YuKi后台管理系统</h1>
      <h1 v-show="store.isCollapse" class="mb-2">YuKi</h1>
      <template v-for="item in menuList" :key="item.path || item.label">
        <!-- 无子菜单 -->
        <el-menu-item v-if="!item.children" :index="item.path" @click="handleMenu(item)">
          <el-icon><component :is="item.icon" /></el-icon>
          <span>{{ item.label }}</span>
        </el-menu-item>

        <!-- 有子菜单 -->
        <el-sub-menu v-else :index="item.label">
          <template #title>
            <el-icon><component :is="item.icon" /></el-icon>
            <span>{{ item.label }}</span>
          </template>
          <el-menu-item
              v-for="child in item.children"
              :key="child.path"
              :index="child.path"
              @click="handleMenu(child)"
          >
            <el-icon><component :is="child.icon" /></el-icon>
            <span>{{ child.label }}</span>
          </el-menu-item>
        </el-sub-menu>
      </template>
    </el-menu>
  </el-aside>
</template>

<style lang="less" scoped>
.el-menu {
  height: 100vh;
  overflow: hidden auto;
  border-right: none;

  h1 {
    height: 60px;
    text-align: center;
    line-height: 60px;
    color: #fff;
    font-size: 18px;
    font-weight: bold;
  }
}
.el-menu::-webkit-scrollbar {
  display: none;
}
</style>